package com.app.service.impl;

import com.app.model.AddressModel;
import com.app.mapper.AddressMapper;
import com.app.service.AddressService;
import com.app.utils.StringTool;
import com.app.vo.AddressVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 地址信息
 *
 * @author 16693103420
 * @since 2024-02-01
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, AddressModel> implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    @Override
    public AddressVo getAddressList(Integer page, Integer limit) {
        Page<AddressModel> page1 = new Page<>(page, limit);

        QueryWrapper<AddressModel> qw = new QueryWrapper<>();
        Page<AddressModel> addressPage = addressMapper.selectPage(page1, qw);
        List<AddressModel> addressList = addressPage.getRecords();

        long count = addressPage.getTotal();
        AddressVo addressVo = new AddressVo();
        addressVo.setList(addressList);
        addressVo.setCount((int) count);
        return addressVo;
    }

    @Override
    public AddressVo getUserAddressList(Integer page, Integer limit, Integer userId) {
        Page<AddressModel> page1 = new Page<>(page, limit);

        QueryWrapper<AddressModel> qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        qw.eq("status", 1);
        Page<AddressModel> addressPage = addressMapper.selectPage(page1, qw);
        List<AddressModel> addressList = addressPage.getRecords();

        long count = addressPage.getTotal();
        AddressVo addressVo = new AddressVo();
        addressVo.setList(addressList);
        addressVo.setCount((int) count);
        return addressVo;
    }
}
